<template>
  <div class="goods-image">
    <!-- 放大后展示的图片 -->
    <!-- middle之前放置大图盒子 -->
    <div
      v-show="!isOutside"
      class="large"
      :style="[
        {
          backgroundImage: `url(${pics[currPicIndex]})`,
          backgroundPosition: `${bg.x}px ${bg.y}px`,
        },
      ]"
    ></div>

    <div class="middle" ref="imgBox">
      <img :src="pics[currPicIndex]" alt="" />
      <!-- 放大镜遮罩层 -->
      <div
        v-show="!isOutside"
        class="layer"
        :style="{ left: `${left}px`, top: `${top}px` }"
      ></div>
    </div>
    <ul class="small">
      <li v-for="(img, i) in pics" :key="i" @mouseenter="currPicIndex = i">
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { ref } from 'vue'
import { useMove } from '@/hook/move.js'
// import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    pics: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    /**
    * 鼠标移入图片列表展示对应的图片
    * 1.每个图片绑定鼠标移入事件
    * 2.鼠标移入某个图片后,修改当前展示的索引值
    */
    const currPicIndex = ref(0)
    const imgBox = ref(null)
    // elementX 鼠标基于容器左上角X轴偏移
    // elementY 鼠标基于容器左上角Y轴偏移
    // isOutside 鼠标是否在模板容器外
    // 三个参数都是ref对象
    /**
     * 控制蒙层移动
     * 1.确定移动坐标范围 => 100< x/y <300
     *  *可移动的盒子区域大小400*400
     *  *被移动蒙层的大小200*200
     * 2.有效范围内坐标如何计算
     *   计算公式:left = elementX -100
     *   计算公式:top = elementY -100
     */
    // const left = ref(0)
    // const top = ref(0)
    // const { elementX, elementY, isOutside } = useMouseInElement(imgBox)
    // watch([elementX, elementY, isOutside], () => {
    //   /**
    //   * 1.控制可移动的范围 =>根据elementX, elementY 值计算得到蒙层的位置=>给left/top赋值
    //   *
    //   * 2.动态绑定left/top值
    //   *
    //   */
    //   //  处理x轴
    //   if (elementX.value < 100) {
    //     left.value = 0
    //   } else if (elementX.value > 300) {
    //     left.value = 200
    //   } else {
    //     left.value = elementX.value - 100
    //   }
    //   // 处理Y轴
    //   if (elementY.value < 100) {
    //     top.value = 0
    //   } else if (elementY.value > 300) {
    //     top.value = 200
    //   } else {
    //     top.value = elementY.value - 100
    //   }
    // })
    const { left, top, bg, isOutside } = useMove(imgBox)

    return { currPicIndex, imgBox, left, top, bg, isOutside }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  // 放大镜
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
